﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Collections;
using System.Net;

namespace MYTech
{
    /// <summary>
    /// 邮件帮助类
    /// </summary>
    public class MailHelper
    {
        private string smtpHostIp;
        private string sender;
        private string pwd;
        private List<string> sendToList;
        private List<string> ccToList;
        private string subject;
        private string body;
        private List<string> paths;
        private bool isBodyHtml;

        public MailHelper(string hostIp, string sender, string senderPswd, string subject, string body)
            : this(hostIp, sender, senderPswd, null, null, subject, body)
        { }

        public MailHelper(string hostIp, string sender, string senderPswd, List<string> sendToList, List<string> ccToList, string subject, string body)
            : this(hostIp, sender, senderPswd, sendToList, ccToList, subject, body, false, null)
        {
        }

        /// <summary>
        /// 初始化邮件
        /// </summary>
        /// <param name="hostIp">邮件服务器IP</param>
        /// <param name="sender">发送者邮箱地址</param>
        /// <param name="senderPswd">发送者密码</param>
        /// <param name="sendToList">收件人列表</param>
        /// <param name="ccToList">抄送收件人列表</param>
        /// <param name="subject">主题</param>
        /// <param name="body">正文内容</param>
        /// <param name="isBodyHtml">正文内容是否以html呈现</param>
        /// <param name="filePaths">附件地址</param>
        public MailHelper(string hostIp, string sender, string senderPswd, List<string> sendToList, List<string> ccToList, string subject, string body, bool isBodyHtml, List<string> filePaths)
        {
            this.smtpHostIp = hostIp;
            this.sender = sender;
            this.pwd = senderPswd;
            this.sendToList = sendToList;
            this.ccToList = ccToList;
            this.subject = subject;
            this.body = body;
            this.isBodyHtml = isBodyHtml;
            this.paths = filePaths;
        }

        /// <summary>
        /// 邮件发送
        /// </summary>
        /// <returns></returns>
        public bool SendMail()
        {
            try
            {
                SmtpClient client = new SmtpClient(smtpHostIp)
                {
                    UseDefaultCredentials = false,
                    Credentials = new NetworkCredential(sender, pwd),
                    DeliveryMethod = SmtpDeliveryMethod.Network
                };
                MailAddress from = new MailAddress(this.sender);
                using (MailMessage message = new MailMessage() { Sender = from, From = from, IsBodyHtml = this.isBodyHtml, BodyEncoding = System.Text.Encoding.UTF8 })
                {
                    foreach (string address in sendToList)
                        message.To.Add(new MailAddress(address, this.sender));
                    if (ccToList != null)
                    {
                        foreach (string ccAddress in ccToList)
                            message.CC.Add(new MailAddress(ccAddress, this.sender));
                    }
                    message.Subject = subject;
                    message.Body = body;
                    if (paths != null && paths.Count != 0)
                    {
                        foreach (string path in paths)
                        {
                            Attachment data = new Attachment(path, System.Net.Mime.MediaTypeNames.Application.Octet);
                            message.Attachments.Add(data);
                        }
                    }
                    client.Send(message);
                    return true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool SendMail(List<string> sendToList)
        {
            this.sendToList = sendToList;
            return SendMail();
        }
    }
}
